﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Mang24k99.Business.COMM;
using Mang24k99.Model.COMM;
using Mang24k99.Model.GOLD;
using Mang24k99.Business.GOLD;

namespace Mang24k99.Web.ProductQuote
{
    public partial class OrglistProductSort : System.Web.UI.Page
    {
        //产品归属类别：1-金商，2-银行
        protected string product_type = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["procduct_type"] != null)
            {
                product_type = Request.QueryString["procduct_type"].ToString();
                if (product_type == "1")
                {
                    this.sort.Visible = false;
                }
            }
            if (!IsPostBack)
            {
                BindOrglist();
                BindGoldType();
                BindGrdviewAdd();
                BindGridView();
            }
        }
        /// <summary>
        /// 绑定机构(银行机构)
        /// </summary>
        public void BindOrglist()
        {
            string where = " orglist.ParentNO=3 and orglist.IsValidate=1 and orglist.IsColumn=1 order by orglist.LevelOrder";
            MKtCommOrglist[] orglist = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
            this.ddlOrglist.DataSource = orglist;
            this.ddlOrglist.DataTextField = "OrgnizeName";
            this.ddlOrglist.DataValueField = "OrgnizeNO";
            this.ddlOrglist.DataBind();
        }

        /// <summary>
        /// 绑定金品分类,目前只对金条和金章首页排序，所以直接绑定这两个分类
        /// </summary>
        public void BindGoldType()
        {
            ddlGoldType.Items.Add(new ListItem("金条", "30204"));
            ddlGoldType.Items.Add(new ListItem("金章", "30207"));
            ddlGoldType.SelectedValue = "30204";
            //MKtCommCodeList[] codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldTypeByBelong(Convert.ToInt32(product_type));
            //this.ddlGoldType.DataSource = codelist;
            //this.ddlGoldType.DataTextField = "CODE_CNAME";
            //this.ddlGoldType.DataValueField = "CODE_NO";
            //this.ddlGoldType.DataBind();
        }
        /// <summary>
        /// 绑定可添加产品列表
        /// </summary>
        public void BindGrdviewAdd()
        {
            int ismporder = 0;
            if (ddlGoldType.SelectedValue=="")
            {
                string strMsg = "金品品种未维护，请先维护金品品种！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            if (ddlOrglist.SelectedValue=="")
            {
                string strMsg = "机构未维护，请先维护机构！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            int orglistno = Convert.ToInt32(ddlOrglist.SelectedValue);
            int goldtype = Convert.ToInt32(ddlGoldType.SelectedValue);
            MKtGoldProduct[] productlist = (MKtGoldProduct[])BKtGoldProduct.SelectOrglistProduct(orglistno, goldtype, ismporder);
            grdAdd.DataSource = productlist;
            grdAdd.DataBind();
        }
        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BindGridView()
        {
            //首页排序的产品
            int ismporder = 1;
            MKtGoldProduct[] productlist = (MKtGoldProduct[])BKtGoldProduct.SelectSortProduct(ismporder);
            if (productlist.Length==0)
            {
                this.Panel2.Visible = false;
            }
            else if (productlist.Length>0)
            {
                this.Panel2.Visible = true;
            }
            grdList.DataSource = productlist;
            grdList.DataBind();
        }
        /// <summary>
        /// 首页排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            int count = this.grdList.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                int NO = Int32.Parse(this.grdList.Rows[i].Cells[1].Text);
                MKtGoldProduct product = BKtGoldProduct.Find(NO);
                GridViewRow gvr = grdList.Rows[i];
                TextBox txtSort = (TextBox)gvr.FindControl("txtSort");
                if (txtSort.Text.Trim() == "")
                {
                    string strMsg = "排序号不能为空，位置第" + (i + 1) + "行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                if (Common.PageValidate.IsNumber(txtSort.Text.Trim()) == false)
                {
                    string strMsg = "排序号必须为正整数，位置第" + (i + 1) + "行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                if (Convert.ToInt32(txtSort.Text.Trim()) == 0)
                {
                    string strMsg = "排序号不能为0，位置第" + (i + 1) + "行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                product.MpageOrder = Convert.ToInt32(txtSort.Text.Trim());
                product.ModifyTime = DateTime.Now;
                try
                {
                    product.Update();
                    string strMsg = "修改成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
                catch (System.Exception ex)
                {

                }
            }
            BindGridView();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void ddlOrglist_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrdviewAdd();
        }

        protected void ddlGoldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrdviewAdd();
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "move")
            {
                int productno = Convert.ToInt32(e.CommandArgument.ToString());
                MKtGoldProduct product = BKtGoldProduct.Find(productno);
                product.Is_Mporder = 0;
                try
                {
                    product.Update();
                    BindGridView();
                    BindGrdviewAdd();
                    string strMsg = "移除成功！";
                    Common.MessageBox.Show(this.Page, strMsg);

                }
                catch (System.Exception ex)
                {

                }
            }
        }

        protected void grdAdd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "add")
            {
                int productno = Convert.ToInt32(e.CommandArgument.ToString());
                MKtGoldProduct product = BKtGoldProduct.Find(productno);
                product.MpageOrder = (int)MKtGoldProduct.GetMaxMpageSortNO() + 1;
                product.Is_Mporder = 1;
                try
                {
                    product.Update();
                    BindGrdviewAdd();
                    BindGridView();
                    string strMsg = "添加成功！";
                    Common.MessageBox.Show(this.Page, strMsg);

                }
                catch (System.Exception ex)
                {

                }

            }

        }
    }
}